﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kortex.Container;
using System.Threading;

namespace Kortex.GUI
{
    public partial class TransctionViewer : Form
    {
        KortexGuiMain RootGui = null ;
        public TransctionViewer()
        {
            InitializeComponent();
        }

        public TransctionViewer(List<ReqRespDebugInfo> req_resp,KortexGuiMain mainGui)
        {
            InitializeComponent();
            RootGui = mainGui;
            updateALL(req_resp);
            AutoScroingInit();
        }

        private void AutoScroingInit()
        {

            this.AutoScroll = true; // For Friends who are running on a low Resolution Monitor

        }

        public void update(ReqRespDebugInfo obj)
        {


            Monitor.Enter(RootGui);
            if (obj.cmd_type == reqResType.type_req)
                RawJson_send_reseice_richBox.AppendText("-->" + obj.mJson + " at time " + obj.Time.Hour + ":" + obj.Time.Minute + ":" + obj.Time.Second + "." + obj.Time.Millisecond + "\n");
            else if (obj.cmd_type == reqResType.type_res)
                RawJson_send_reseice_richBox.AppendText("<--" + obj.mJson + " at time " + obj.Time.Hour + ":" + obj.Time.Minute + ":" + obj.Time.Second + "." + obj.Time.Millisecond + "\n");
            Monitor.Exit(RootGui);

        }

        public void updateALL(List<ReqRespDebugInfo> req_resp)
        {
            int i =0;

            Monitor.Enter(RootGui);
            foreach (ReqRespDebugInfo obj in req_resp)
            {
                if (obj.cmd_type == reqResType.type_req)
                    RawJson_send_reseice_richBox.AppendText("-->" + obj.mJson + " at time " + obj.Time.Hour + ":" + obj.Time.Minute + ":" + obj.Time.Second + "." + obj.Time.Millisecond + "\n");
                else if (obj.cmd_type == reqResType.type_res)
                    RawJson_send_reseice_richBox.AppendText("<--" + obj.mJson + " at time " + obj.Time.Hour + ":" + obj.Time.Minute + ":" + obj.Time.Second + "." + obj.Time.Millisecond + "\n");

                KpTreeViewer.addNode(obj);
                i++;
            }

            Monitor.Exit(RootGui);
            
        }

        private void ClearAll_Click(object sender, EventArgs e)
        {
            RootGui.clearTransction();
            RawJson_send_reseice_richBox.Clear();
        }

        private void TransctionViewer_FormClosed(object sender, FormClosedEventArgs e)
        {
            RootGui.TransctionViewerClosed();

        }

        private void TransctioN_reload_Click(object sender, EventArgs e)
        {
            RawJson_send_reseice_richBox.Clear();
            RootGui.ReloadTrasction();

        }


    }
}
